package com.fw.ui.view.profile.service.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.service.formvalidator.FormValidatorService;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.view.event.AbstractListener;
import com.fw.ui.view.profile.EditCashServiceProfileView;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.validator.RegexpValidator;

@Component
@Scope(SCOPE_PROTOTYPE)
public class EditCashServiceProfileIsSuccessUrlValueChangeListener extends AbstractListener implements ValueChangeListener {

    private static final long serialVersionUID = 1L;

    @Autowired
    private MessageBAO messageBAO;

    @Override
    public void valueChange(ValueChangeEvent event) {
	final EditCashServiceProfileView editCashServiceProfileView = (EditCashServiceProfileView) this.getBasedComponent();
	Boolean selected = (Boolean) event.getProperty().getValue();
	if (selected) {
	    editCashServiceProfileView.getTxtSuccessUrl().setImmediate(true);
	    editCashServiceProfileView.getTxtSuccessUrl().setReadOnly(false);
	    editCashServiceProfileView.getTxtSuccessUrl().removeAllValidators();
	    editCashServiceProfileView.getTxtSuccessUrl().setRequired(true);
	    editCashServiceProfileView.getTxtSuccessUrl().setRequiredError(
		    this.messageBAO.getMessage("com.fw.ui.view.profile.EditCashServiceProfileView.message.successUrl.mandatory"));
	    RegexpValidator successUrlRegexpValidator = new RegexpValidator(FormValidatorService.PATTERN_URL, true,
		    this.messageBAO.getMessage("com.fw.ui.view.profile.EditCashServiceProfileView.message.successUrl.format"));
	    editCashServiceProfileView.getTxtSuccessUrl().addValidator(successUrlRegexpValidator);
	    editCashServiceProfileView.getTxtSuccessUrl().setValidationVisible(true);
	} else {
	    editCashServiceProfileView.getTxtSuccessUrl().setImmediate(true);
	    editCashServiceProfileView.getTxtSuccessUrl().setComponentError(null);
	    editCashServiceProfileView.getTxtSuccessUrl().setValidationVisible(false);
	    editCashServiceProfileView.getTxtSuccessUrl().setValue("");
	    editCashServiceProfileView.getTxtSuccessUrl().setRequired(false);
	    editCashServiceProfileView.getTxtSuccessUrl().setRequiredError("");
	    editCashServiceProfileView.getTxtSuccessUrl().removeAllValidators();
	    editCashServiceProfileView.getTxtSuccessUrl().setReadOnly(true);
	    editCashServiceProfileView.getTxtSuccessUrl().removeStyleName("v-textfield-error");
	    editCashServiceProfileView.getLblSuccessUrlValidateMessage().setValue("");
	    editCashServiceProfileView.getLayoutSuccessUrlValidate().setVisible(false);
	}
    }
}
